package com.school.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.school.entity.HljtDevice;
import com.school.entity.web.HljtDeviceManagement;
import com.school.service.HljtDeviceService;
import com.school.util.Const;
import com.school.util.JsonUtil;
import com.school.util.RepUtil;

@Controller
public class HljtDeviceController {
	@Autowired
	private HljtDeviceService hljtDeviceService;
	
	/**
	 * 判断设备是否注册
	 * @param hljtDevice
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public JSONObject login(HljtDevice hljtDevice ,JsonUtil jsonUtil) {
		try {
			if(null != hljtDevice.getIp() && null != hljtDevice.getMac()) {
				HljtDevice hljtDeviceE =  hljtDeviceService.equipmentRegistration(hljtDevice);
				if(null == hljtDeviceE) {
					hljtDevice.setRegtime(new Date());
					hljtDevice.setEndtime(new Date());
					hljtDevice.setUpdate(1);
					hljtDevice.setPattern(1);
					hljtDevice.setSwitchgear(1);
					hljtDevice.setSink(1);
					int temp = hljtDeviceService.addDevice(hljtDevice);
					hljtDevice.setMac(null);
					if(-1==temp) {
						JSONObject j = new JSONObject();
						j.put("register", Const.FAIL);
						j.put("registerMsg", "注册失败");
						jsonUtil.setSucceed(j.toJSONString(),"请求成功");
					}else {
						JSONObject j = new JSONObject();
						j.put("register", Const.SUCCEED);
						j.put("registerMsg", "注册成功");
						jsonUtil.setSucceed(j.toJSONString(),"请求成功");
					}
				}else {
					JSONObject j = new JSONObject();
					j.put("registerInformation", hljtDeviceE);
					j.put("register", Const.FAIL);
					j.put("registerMsg", "已注册");
					jsonUtil.setSucceed(j.toJSONString(), "请求成功");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setServerException("服务器异常");
		}
		
		 return jsonUtil.toJSONString();
		
	}
	
	
	
	/**
	 * 没有绑定班级的设备信息
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/deviceNotBoundClassInfo")
	@ResponseBody
	public JSONObject deviceNotBoundClassInfo(HttpServletResponse rep , HttpServletRequest request,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			ret.put("data", hljtDeviceService.deviceNotBoundClass());
		} catch (Exception e) {
			e.printStackTrace();
			//jsonUtil.setFailMsg("服务器异常");
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	
	

	/**
	 * 绑定班级的设备信息
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/deviceBindClassInfo")
	@ResponseBody
	public JSONObject deviceNotBoundClassInfo(HttpServletResponse rep ,HljtDeviceManagement hljtDeviceManagement , HttpServletRequest request,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			hljtDeviceManagement.setPageNum((hljtDeviceManagement.getPageIndex()-1)*hljtDeviceManagement.getPageSize());
			
				ret.put("deviceInfo", hljtDeviceService.deviceBindClassInfo(hljtDeviceManagement));
		} catch (Exception e) {
			e.printStackTrace();
			//jsonUtil.setFailMsg("服务器异常");
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	


	
	
	/**
	 * 解出绑定关系
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/unbindingRelationship")
	@ResponseBody
	public JSONObject unbindingRelationship(String data , HttpServletResponse rep  , HttpServletRequest request,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			HljtDevice hljtDeviceManagement = JSON.parseObject(data, HljtDevice.class); 
			Integer dev = null;
			if(null != hljtDeviceManagement) {
			 dev = hljtDeviceService.updateDeviceByMavAndClassId(hljtDeviceManagement);
			}
			if(dev>0) {
				ret.put("success", true);
			}else {
				ret.put("success",false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	/**
	 * 绑定班级和设备
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/classBindingDevice")
	@ResponseBody
	public JSONObject classBindingDevice(String data , HttpServletResponse rep  , HttpServletRequest request,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		try {
			RepUtil.repPake(rep);
			List<HljtDeviceManagement> hljtDeviceManagement = JSON.parseArray(data, HljtDeviceManagement.class);  
			List<HljtDeviceManagement>list = new ArrayList<>();
			List<HljtDeviceManagement>ss = new ArrayList<>();
			Integer dev = null;
			if( null !=hljtDeviceManagement && hljtDeviceManagement.size()>0) {
				for (HljtDeviceManagement temp : hljtDeviceManagement) {
					temp.setPattern(2);
					list = temp.getModulesList();
					if(null !=list&&list.size()>0) {
						for (HljtDeviceManagement hlj : list) {
							hlj.setClassOnlyId(temp.getClassOnlyId());
							hlj.setMac(temp.getMac());
							ss.add(hlj);
						}
					}else {
						HljtDeviceManagement t = new HljtDeviceManagement();
						t.setClassOnlyId(temp.getClassOnlyId());
						t.setMac(temp.getMac());
						ss.add(t);
					}
					if(null !=temp.getOldClassOnlyId()) {
						HljtDeviceManagement f = new HljtDeviceManagement();
						f.setClassOnlyId(temp.getOldClassOnlyId());
						f.setMac(temp.getMac());
						ss.add(f);
					}
					
				}
				dev = hljtDeviceService.updateDeviceByMac(hljtDeviceManagement);
			}
			boolean b = false;
			if( null !=ss && ss.size()>0) {
				b = hljtDeviceService.addDevicetask(ss);
			}else {
				b = true;
			}
			
			if(dev>0&&b) {
				ret.put("success", true);
			}else {
				ret.put("success",false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
	}
	
	
	
	
	/**
	 * 修改皮肤
	 * @param hljtSchoolimage mac sink
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/updatesStyle")
	@ResponseBody
	public JSONObject updatesStyle(HljtDevice hljtDevice, HttpServletResponse rep  , HttpServletRequest request,JsonUtil jsonUtil) {
		
		try {
			if(null!=hljtDevice&&null!=hljtDevice.getMac()&&null!=hljtDevice.getSink()) {
				Integer temp = hljtDeviceService.updateDevice(hljtDevice);
				if(temp>0) {
					jsonUtil.setSucceed("样式更换成功","修改成功");
				}else {
					jsonUtil.setSucceed("样式更换失败","修改失败");
				}	
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setServerException("服务器异常");
		}
		return jsonUtil.toJSONString();
	}
	
	
	
	
	/**
	 * 修改设备状态
	 * @param hljtSchoolimage mac sink
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/updateDeviceStateByEndTime")
	@ResponseBody
	public void updateDeviceStateByEndTime(JsonUtil jsonUtil) {
		
		try {
	           Integer t =hljtDeviceService.updateDeviceStateByEndTime(); 
	           jsonUtil.setSucceed(t, "影响的行数");
	           System.out.println(jsonUtil.toString());
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
}
